package cn.bocweb.spinning.view.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.bocweb.spinning.R;
import cn.bocweb.spinning.model.Bean.ForumMyCollectBean;
import cn.bocweb.spinning.view.common.GlideCircle;
import cn.bocweb.spinning.view.common.MyAdapter;

/**
 * Created by chenchangxing on 15/12/21.
 */
public class CollectTopicAdapter extends MyAdapter<ForumMyCollectBean.DataBean> {

    public Map<Integer, Boolean> map;

    public boolean isSetAllSelect() {
        return setAllSelect;
    }

    public void setSetAllSelect(boolean setAllSelect) {
        this.setAllSelect = setAllSelect;
    }

    boolean setAllSelect = false;

    public boolean isShowSelect() {
        return showSelect;
    }

    public void setShowSelect(boolean showSelect) {
        this.showSelect = showSelect;
    }

    CheckBox mainCheckBox;

    boolean showSelect = false;

    public Map<Integer, Boolean> getMap() {
        return map;
    }

    public void setMap(Map<Integer, Boolean> map) {
        this.map = map;
    }

    public CollectTopicAdapter(Context context, List<ForumMyCollectBean.DataBean> list, CheckBox mainCheckBox) {
        super(context, list);
        map = new HashMap<>();
        this.mainCheckBox = mainCheckBox;

        for (int i = 0; i < list.size(); i++) {
            map.put(i, false);
        }
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_collect_topic, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        Glide
                .with(context)
                .load(list.get(position).getUthumb())
                .placeholder(R.mipmap.login_head)
                .transform(new GlideCircle(context))
                .into(holder.mIcon);
        holder.mNickname.setText(list.get(position).getNickname());
        holder.mContent.setText(list.get(position).getContent());
        holder.mPosition.setText(String.valueOf(list.get(position).getCount()));
        List<ImageView> images = new ArrayList<>();
        images.add(holder.mImg1);
        images.add(holder.mImg2);
        images.add(holder.mImg3);
        if (list.get(position).getPhoto() != null) {
            for (int i = 0; i < list.get(position).getPhoto().size(); i++) {
                Glide
                        .with(context)
                        .load(list.get(position).getPhoto().get(i))
                        .into(images.get(i));
            }
        } else {
            holder.mImgLayout.setVisibility(View.GONE);
        }

        holder.mCollectInfoSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                map.put(position, isChecked);
                if (!isChecked && setAllSelect) {
                    setAllSelect = false;
                    mainCheckBox.setChecked(false);
                }
            }
        });

        if (showSelect) {
            holder.mCollectInfoSelect.setVisibility(View.VISIBLE);
            holder.mCollectInfoSelect.setChecked(setAllSelect);
        } else {
            holder.mCollectInfoSelect.setVisibility(View.GONE);
        }

        return convertView;
    }

    static class ViewHolder {
        @Bind(R.id.collect_info_select)
        CheckBox mCollectInfoSelect;
        @Bind(R.id.icon)
        ImageView mIcon;
        @Bind(R.id.nickname)
        TextView mNickname;
        @Bind(R.id.position)
        TextView mPosition;
        @Bind(R.id.content)
        TextView mContent;
        @Bind(R.id.img1)
        ImageView mImg1;
        @Bind(R.id.img2)
        ImageView mImg2;
        @Bind(R.id.img3)
        ImageView mImg3;
        @Bind(R.id.img_layout)
        LinearLayout mImgLayout;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }


    /**
     * This class contains all butterknife-injected Views & Layouts from layout file 'item_collect_topic.xml'
     * for easy to all layout elements.
     *
     * @author ButterKnifeZelezny, plugin for Android Studio by Avast Developers (http://github.com/avast)
     */

}
